package com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.Impl;



import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_SaveSCDailyContextEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_SysAuthAuditMessageEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_DatesEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.MongoDB_S_C_SaveDailyContextDao;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MongoDB_S_C_SaveDailyContextDaoImpl implements MongoDB_S_C_SaveDailyContextDao {
    @Resource
    MongoTemplate mongoTemplate;


    @Override
    public boolean saveSCDailyContext(MongoDB_S_C_SaveSCDailyContextEntity mongoDB_s_c_saveSCDailyContextEntity) {
        MongoDB_S_C_SaveSCDailyContextEntity save = mongoTemplate.save(mongoDB_s_c_saveSCDailyContextEntity);
        return save.getPermission_tbEntities().get(0).getPermissionId() != null;
    }

    @Override
    public List<MongoDB_S_C_SaveSCDailyContextEntity> findS_C_DailyAuditMessage(Permission_TBEntity permission_tbEntity) {
        Query query = new Query();
        List<MongoDB_S_C_SaveSCDailyContextEntity> mongoDB_S_C_SaveSCDailyContextEntity = null;

        query.addCriteria(Criteria.where("permission_tbEntities.permission").is(permission_tbEntity.getPermission()));

        mongoDB_S_C_SaveSCDailyContextEntity = mongoTemplate.find(query, MongoDB_S_C_SaveSCDailyContextEntity.class);
        return mongoDB_S_C_SaveSCDailyContextEntity;
    }

    @Override
    public List<MongoDB_S_C_SaveSCDailyContextEntity> findS_C_DailyAuditMessage(Permission_TBEntity permission_TBEntity, Date date, String pointName) {
        Query query = new Query();
        List<MongoDB_S_C_SaveSCDailyContextEntity> mongoDB_S_C_SaveSCDailyContextEntity = null;

        query.addCriteria(Criteria.where("permission_tbEntities.permission").is(permission_TBEntity.getPermission())
                        .andOperator(Criteria.where("sCDatesEntityS.date").is(date)
                        .andOperator(Criteria.where("sCDatesEntityS.point").is(pointName))));

        mongoDB_S_C_SaveSCDailyContextEntity = mongoTemplate.find(query, MongoDB_S_C_SaveSCDailyContextEntity.class);
        return mongoDB_S_C_SaveSCDailyContextEntity;
    }

    @Override
    public MongoDB_S_C_SysAuthAuditMessageEntity getUserAudit(String userName) {
        Query query = new Query();
        query.addCriteria(Criteria.where("existUserEntity.uname").is(userName));
        List<MongoDB_S_C_SysAuthAuditMessageEntity> mongoDB_s_c_sysAuthAuditMessageEntities = mongoTemplate.find(query, MongoDB_S_C_SysAuthAuditMessageEntity.class);
        if (mongoDB_s_c_sysAuthAuditMessageEntities.isEmpty()){
            return null;
        }else {
            mongoTemplate.remove(query,MongoDB_S_C_SysAuthAuditMessageEntity.class);
            return mongoDB_s_c_sysAuthAuditMessageEntities.get(0);
        }

    }

    @Override
    public boolean auditNGBtnFun(String userName) {
        Query query = new Query();
        query.addCriteria(Criteria.where("existUserEntity.uname").is(userName));
        DeleteResult remove = mongoTemplate.remove(query,MongoDB_S_C_SysAuthAuditMessageEntity.class);
        return remove.getDeletedCount() > 0;

    }



    @Override
    public List<MongoDB_S_C_SaveSCDailyContextEntity> findMongoDB_s_c_saveDailyContextEntity(Permission_TBEntity permission_tbEntity) {
        Query query = new Query();
        query.addCriteria(Criteria.where("permission_tbEntities.permission").is(permission_tbEntity.getPermission()));

        return mongoTemplate.find(query, MongoDB_S_C_SaveSCDailyContextEntity.class);

    }

    @Override
    public List<MongoDB_S_C_SaveSCDailyContextEntity> findMongoDBSCDaily(String pointName, Date stringToDate) {
        Query query = new Query();
        query.addCriteria(Criteria.where("sCDatesEntityS.point").is(pointName).andOperator(Criteria.where("sCDatesEntityS.date").is(stringToDate)));
        return mongoTemplate.find(query,MongoDB_S_C_SaveSCDailyContextEntity.class);
    }

    @Override
    public boolean updateSCDailyContext(MongoDB_S_C_SaveSCDailyContextEntity mongoDB_s_c_saveSCDailyContextEntity) {
        Query query = new Query();
        query.addCriteria(Criteria.where("sCDatesEntityS.point").is(mongoDB_s_c_saveSCDailyContextEntity.getSCDatesEntityS().get(0).getPoint())
                .andOperator(Criteria.where("sCDatesEntityS.date").is(mongoDB_s_c_saveSCDailyContextEntity.getSCDatesEntityS().get(0).getDate())));
        DeleteResult remove = mongoTemplate.remove(query,MongoDB_S_C_SaveSCDailyContextEntity.class);
        if (remove.getDeletedCount() > 0) {
            MongoDB_S_C_SaveSCDailyContextEntity save = mongoTemplate.save(mongoDB_s_c_saveSCDailyContextEntity);
            return true;
        }else {
            return false;
        }


    }

    @Override
    public boolean deleteS_C_DailyAuditMessage(Permission_TBEntity permission_tbEntity, Date date1, String pointName) {
        Query query = new Query();
        query.addCriteria(Criteria.where("permission_tbEntities.permission").is(permission_tbEntity.getPermission())
                .andOperator(Criteria.where("sCDatesEntityS.date").is(date1)
                        .andOperator(Criteria.where("sCDatesEntityS.point").is(pointName))));
        DeleteResult remove = mongoTemplate.remove(query, MongoDB_S_C_SaveSCDailyContextEntity.class);

        return remove.getDeletedCount() > 0;
    }

    @Override
    public boolean acceptWorkOrders(Integer uid, String scSailings, String date, String pointName) {
        Date date1 = new Date();
        date1.setTime(Long.parseLong(date));
        Query query = new Query();
        query.addCriteria(Criteria.where("sCDatesEntityS.point").is(pointName).andOperator(Criteria.where("sCDatesEntityS.date").is(date1)));
        List<MongoDB_S_C_SaveSCDailyContextEntity> mongoDB_s_c_webSocketMessageEntities = mongoTemplate.find(query, MongoDB_S_C_SaveSCDailyContextEntity.class);
        boolean isEmpty = false;
        isEmpty = mongoDB_s_c_webSocketMessageEntities.size() > 0;
        UpdateResult updateResult = null;

        Update update = new Update();
        if (isEmpty){
            List<S_C_DatesEntity> s_c_datesEntities = mongoDB_s_c_webSocketMessageEntities.get(0).getSCDatesEntityS();

            boolean b = (scSailings.equals("早班"));
            System.out.println(scSailings);
            for (S_C_DatesEntity s_c_datesEntity : s_c_datesEntities) {
                if (b){
                    s_c_datesEntity.setMorningExceptionCorrespondence(uid);
                }else {
                    s_c_datesEntity.setMiddayExceptionCorrespondence(uid);
                }

            }
            update.set("sCDatesEntityS",s_c_datesEntities);



            updateResult = mongoTemplate.updateMulti(query, update, MongoDB_S_C_SaveSCDailyContextEntity.class);
        }


        if (updateResult != null){
            return updateResult.getModifiedCount() > 0;
        }else {
            return false;
        }

    }
    @Override
    public List<MongoDB_S_C_SaveSCDailyContextEntity> findS_C_ExMessage(Permission_TBEntity permission_tbEntity) {
        Query query = new Query();
        query.addCriteria(Criteria.where("permission_tbEntities.permission").is(permission_tbEntity.getPermission()));
        return mongoTemplate.find(query,MongoDB_S_C_SaveSCDailyContextEntity.class);
    }

    @Override
    public boolean reviseStatusDetermine(String SCStatusSelect, String scSailings, Long date, String pointContent) {
        Query query = new Query();
        Date date1 = new Date();
        date1.setTime(date);
        int StatusSelect = Integer.parseInt(SCStatusSelect);
        query.addCriteria(Criteria.where("permission_tbEntities.permission").is("s5").andOperator(Criteria.where("sCDatesEntityS.date").is(date1),Criteria.where("sCDatesEntityS.pointContent").is(pointContent)));
        List<MongoDB_S_C_SaveSCDailyContextEntity> sCSaveSCDailyContextEntities = mongoTemplate.find(query, MongoDB_S_C_SaveSCDailyContextEntity.class);
        boolean b = scSailings.equals("早班");
        Update update = new Update();

        if (b){
            update.set("sCDatesEntityS.$.pointContentMorningStatusId",StatusSelect);
        }else {
            update.set("sCDatesEntityS.$.pointContentMiddayStatusId",StatusSelect);

        }
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, MongoDB_S_C_SaveSCDailyContextEntity.class);
        System.out.println(updateResult);
        return updateResult.getModifiedCount() > 0;
    }

}
